package com.jf.docsify.modules.orgz;

import java.util.List;

import org.apache.commons.collections4.CollectionUtils;

import com.jf.docsify.common.exception.DocsifyException;
import com.jf.docsify.common.utils.AssertUtil;
import com.jf.docsify.modules.model.Organization;

/**
 * Description:
 *
 * @author chenwr
 * @version 1.0
 *
 * <pre>
 * 修改记录:
 * 修改后版本           修改人       修改日期         修改内容
 * 2022/7/29.1       chenwr    2022/7/29           Create
 * </pre>
 * @date 2022/7/29
 */
public class OrganizationService {

    private Organization dao = new Organization().dao();


    public List<Organization> findByUserId(String userId) {
        AssertUtil.hasText(userId, "用户Id 不能为空");
        return dao.find("select * from doc_organization where user_id = ?", userId);
    }

    public boolean save(String organizationName, String path, Integer userId, String remark) {
        List<Organization> organizations = dao.find("select * from doc_organization where path = ?", path);
        if (CollectionUtils.isNotEmpty(organizations)) {
            DocsifyException.throwE("存在相同的组织地址，请重新设置");
        }
        Organization organization = new Organization();
        organization.setOrganizationName(organizationName);
        organization.setPath(path);
        organization.setUserId(userId);
        organization.setRemark(remark);
        return organization.save();
    }

    public boolean saveOrUpdate(Organization organization) {
        Integer id = organization.getId();
        if (id == null) {
            return save(organization.getOrganizationName(), organization.getPath(), organization.getUserId(), organization.getRemark());
        }
        return organization.update();
    }

    public Organization findById(String id) {
        return dao.findById(id);
    }
}